package com.xls.box2023.wxshare;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;

import com.lzz.bottomencapsulation.BuildConfig;
import com.lzz.bottomencapsulation.net.RequestCallBack;
import com.lzz.bottomencapsulation.net.RetrofitBase;
import com.lzz.bottomencapsulation.utils.TUtils;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.xls.box2023.BoxAppLication;
import com.xls.box2023.entity.UserEntity;
import com.xls.box2023.entity.YouXiEntity;
import com.xls.box2023.network.RequestManager;
import com.xls.box2023.network.RequestManager4;

import java.util.List;

public class WxShareUtils {

    /**
    * 单例模式
    **/
    private static WxShareUtils mWxShareUtils = null;
        /**
         * 构造函数私有化
         **/
        private WxShareUtils() {
        }
        /**
         * 公有的静态函数，对外暴露获取单例对象的接口
         **/
        public static WxShareUtils getInstance() {
            if (mWxShareUtils == null) {
                synchronized (WxShareUtils.class) {
                    if (mWxShareUtils == null) {
                        mWxShareUtils = new WxShareUtils();
                    }
                }
            }
            return mWxShareUtils;
        }
    private static final int THUMB_SIZE = 150;
    public void shareBitmap(Context context, String appId, Bitmap bitmap,String id,String openid){
        // 通过appId得到IWXAPI这个对象
        IWXAPI wxapi = WXAPIFactory.createWXAPI(context, appId);
        // 检查手机或者模拟器是否安装了微信
        if (!wxapi.isWXAppInstalled()) {
            TUtils.showShort(context,"您还没有安装微信");
            return;
        }

        //初始化 WXImageObject 和 WXMediaMessage 对象
        WXImageObject imgObj = new WXImageObject(bitmap);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

//设置缩略图
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, THUMB_SIZE, THUMB_SIZE, true);
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

//构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = id;
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        req.userOpenId = openid;
//调用api接口，发送数据到微信
        wxapi.sendReq(req);
    }

    public void getUserCode(RequestCallBack<UserEntity> callBack) {
        RetrofitBase.getInstace(BoxAppLication.getBaseApplication()).setSubscribe(BoxAppLication.getBaseApplication(), RequestManager.getInstance(BoxAppLication.getBaseApplication()).
                getUserCode(), callBack);
    }


    public void getUserCode(RequestCallBack<String> callBack,String apkName,String userCode) {
        RetrofitBase.getInstace(BoxAppLication.getBaseApplication()).setSubscribe(BoxAppLication.getBaseApplication(), RequestManager4.getInstance(BoxAppLication.getBaseApplication()).
                getUserCode(apkName,userCode, BuildConfig.KEYSTORE), callBack);
    }


    /**
     * 判断 用户是否安装微信客户端
     */
    public  boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager(); //获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }
        return false;
    }

    public void getUserCode2(RequestCallBack<YouXiEntity> callBack,String apkName,String userCode,String jksName){
        RetrofitBase.getInstace(BoxAppLication.getBaseApplication()).setSubscribe(BoxAppLication.getBaseApplication(), RequestManager4.getInstance(BoxAppLication.getBaseApplication()).
                getUserCode2(apkName,userCode,jksName), callBack);
    }
}